/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.asiainfo.utils.sql.mysql;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MySQL {
	private Connection conn = null;
	private String server;
	private String port;
	private String dbname;
	private String user;
	private String pass;

	public MySQL(String server_c, String port_c, String dbname_c, String user_c, String pass_c) {
		try {
			this.server = server_c;
			this.port = port_c;
			this.dbname = ((dbname_c == null) ? "" : dbname_c);
			this.user = URLEncoder.encode(user_c, "UTF-8");
			this.pass = URLEncoder.encode(pass_c, "UTF-8");
		} catch (UnsupportedEncodingException ex) {
			Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public Connection getConnection() throws SQLException {
		if (this.conn == null) {
			String connString = new StringBuilder().append("jdbc:mysql://").append(this.server).append(":")
					.append(this.port).append("/").append(this.dbname).append("?user=").append(this.user)
					.append("&password=").append(this.pass).append("&useUnicode=true&characterEncoding=UTF-8")
					.toString();

			Connection con = null;
			con = DriverManager.getConnection(connString);
			this.conn = con;
		}
		return this.conn;
	}

	public PreparedStatement getPreparedStatement(String sql) throws SQLException {
		PreparedStatement ps = null;
		ps = getConnection().prepareStatement(sql);
		ps.setFetchSize(2147483647);
		return ps;
	}

	public Statement getStatement() throws SQLException {
		Statement sta = null;
		sta = getConnection().createStatement();
		sta.setFetchSize(-2147483648);
		return sta;
	}

	public ResultSet query(String sql) throws SQLException {
		ResultSet rs = null;
		PreparedStatement ps = null;
		ps = getPreparedStatement(sql);
		rs = ps.executeQuery();
		return rs;
	}

	public List<HashMap> queryList(String sql) throws SQLException {
		return getRows(query(sql));
	}

	public HashMap find(String sql) throws SQLException {
		return getRow(query(sql));
	}

	public HashMap find(ResultSet rs) throws SQLException {
		return getRow(rs);
	}

	public int execSQL(String sql) throws SQLException {
		int affectNum = 0;
		PreparedStatement ps = null;
		ps = getPreparedStatement(sql);
		affectNum = ps.executeUpdate();
		closeAll(null, ps, null);
		return affectNum;
	}

	public void closeAll(ResultSet rs, PreparedStatement ps, Connection con) {
		if (rs != null)
			try {
				rs.close();
			} catch (SQLException ex) {
			}
		if (ps != null)
			try {
				ps.close();
			} catch (SQLException ex) {
			}
		if (con == null)
			return;
		try {
			con.close();
		} catch (SQLException ex) {
		}
	}

	public void close() {
		if (this.conn == null)
			return;
		try {
			this.conn.close();
		} catch (SQLException ex) {
		}
	}

	public List<HashMap> getRows(ResultSet rs) throws SQLException {
		ArrayList ret = new ArrayList();
		ArrayList rsColNames = new ArrayList();
		if (rs != null) {
			while (rs.next()) {
				rsColNames = getColumns(rs);

				Map line = new HashMap();
				for (int i = 0; i < rsColNames.size(); ++i) {
					line.put(rsColNames.get(i).toString(), rs.getObject(rsColNames.get(i).toString()));
				}
				ret.add(line);
			}
			closeAll(rs, null, null);
			return ret;
		}
		return null;
	}

	public HashMap getRow(ResultSet rs) throws SQLException {
		ArrayList rsColNames = new ArrayList();
		if ((rs != null) && (rs.next())) {
			rsColNames = getColumns(rs);
			HashMap line = new HashMap();
			for (int i = 0; i < rsColNames.size(); ++i) {
				line.put(rsColNames.get(i).toString(), rs.getObject(rsColNames.get(i).toString()));
			}
			closeAll(rs, null, null);
			return line;
		}

		return null;
	}

	public ArrayList getColumns(ResultSet rs) throws SQLException {
		ArrayList ret = new ArrayList();
		ResultSetMetaData rsmd = rs.getMetaData();

		int k = rsmd.getColumnCount();
		for (int i = 1; i <= k; ++i) {
			ret.add(rsmd.getColumnName(i));
		}
		return ret;
	}

	public static String escape(String s) {
		int length = s.length();
		int newLength = length;
		for (int i = 0; i < length; ++i) {
			char c = s.charAt(i);
			switch (c) {
				case '\0' :
				case '"' :
				case '\'' :
				case '\\' :
					++newLength;
			}

		}

		if (length == newLength) {
			return s;
		}
		StringBuilder sb = new StringBuilder(newLength);
		for (int i = 0; i < length; ++i) {
			char c = s.charAt(i);
			switch (c) {
				case '\\' :
					sb.append("\\\\");

					break;
				case '"' :
					sb.append("\\\"");

					break;
				case '\'' :
					sb.append("\\'");

					break;
				case '\0' :
					sb.append("\\0");

					break;
				default :
					sb.append(c);
			}
		}

		return sb.toString();
	}

	static {
		String driverName = "com.mysql.jdbc.Driver";
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}